//
//  HXSelectedPersonResultView.m
//  KD
//
//  Created by ifly on 16/2/26.
//  Copyright © 2016年 ifly. All rights reserved.
//

#import "HXSelectedPersonResultView.h"
#import "HXSelectedPersonTableViewCell.h"
//#import "HXSelectedPersonViewController.h"

@interface HXSelectedPersonResultView()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation HXSelectedPersonResultView
+(instancetype)viewWithXIB:(HXSelectedPersonViewController *)vc{
    HXSelectedPersonResultView *view = [[[NSBundle mainBundle]loadNibNamed:@"HXSelectedPersonResultView" owner:vc options:nil]lastObject];
    view.tableView.delegate = view;
    view.tableView.dataSource = view;
    view.tableView.rowHeight = 53;
    view.tableView.showsHorizontalScrollIndicator = NO;
    view.tableView.showsVerticalScrollIndicator = NO;
    view.personNameArray = [[NSMutableArray alloc]init];
//    view.tableView.mj_footer=[AWRefreshFooder footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    view.tableView.layer.cornerRadius = 7;
    view.okBtn.layer.cornerRadius = 7;
    view.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    view.personArray = personArray;
//    view.tableView.mj_header=[AWRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(regreshData)];
//    view.tableView.mj_footer=[AWRefreshFooder footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
//    view.tableView.mj_footer.automaticallyChangeAlpha = YES;
//    view.tableView.mj_footer.automaticallyHidden = NO;
//    [view.tableView.mj_header beginRefreshing];
//    view.tableView.mj_footer.automaticallyChangeAlpha = YES;
//    view.tableView.mj_footer.automaticallyHidden = NO;
    return view;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.cellType isEqualToString:@"zhuguan"]) {
        HXSelectedPersonTableViewCell *cell = [HXSelectedPersonTableViewCell manageCellWithTableView:tableView indexPath:indexPath data:self.data[indexPath.row]];
        return cell;
    }else{
            HXSelectedPersonTableViewCell *cell = [HXSelectedPersonTableViewCell cellWithTableView:tableView indexPath:indexPath data:self.data[indexPath.row]];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HXSelectedPersonTableViewCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
    if ([cell.data.isSelected isEqualToString:@"no"]||[cell.manageData.isSelected isEqualToString:@"no"]) {
        cell.data.isSelected = @"yes";
        cell.manageData.isSelected = @"yes";
        cell.isSelected = YES;
    }else if ([cell.data.isSelected isEqualToString:@"yes"]||[cell.manageData.isSelected isEqualToString:@"yes"]){
        cell.data.isSelected = @"no";
        cell.manageData.isSelected = @"no";
        cell.isSelected = NO;
    }else{
        cell.data.isSelected = @"no";
        cell.manageData.isSelected = @"no";
        cell.isSelected = NO;
    }
//    if (cell.isSelected) {//若果是选中的 判断有没有 没有加进去
//        HXLog(@"选中了%@",cell.data.ID);
//        for (NSString *str in self.personIDArray) {
//            if ([str isEqualToString:cell.data.ID])return;//找到了
//        }
//        [self.personIDArray addObject:cell.data.ID];
//    }else{//未选中  判断有没有  有就删掉
//        HXLog(@"取消了%@",cell.data.ID);
//        NSArray *array = [NSArray arrayWithArray:self.personIDArray];
//        for (NSString *str in array) {
//            if ([str isEqualToString:cell.data.ID])[self.personIDArray removeObject:str];//找到了
//        }
//        //下面这么写 配合上面的if会出现崩溃
//        /**要考虑你的多线程的问题. 一般是你的一个线程正在对这个array进行遍历,而另外一个线程同时对这个array进行了修改*/
////        for (NSString *str in self.personIDArray) {
////            if ([str isEqualToString:cell.data.ID])[self.personIDArray removeObject:str];//找到了
////        }
//    }
}
- (IBAction)okBtnClick:(id)sender {
    [self.delegate selectedPersonOkBtnClick];
}

- (void)setData:(NSArray *)data{
    _data = data;
    [self.tableView reloadData];
}

- (NSMutableArray *)personIDArray{
    if (!_personIDArray) {
        _personIDArray = [[NSMutableArray alloc]init];
    }
    [_personIDArray removeAllObjects];
    for (HXPersonSelectedListModel *model in self.data) {
        if ([model.isSelected isEqualToString:@"yes"]) {
            [_personIDArray addObject:model.ID];
            [self.personNameArray addObject:model.PersonName];
        }
    }
    return _personIDArray;
}

- (NSMutableArray *)managePersonIDArray{
    if (!_managePersonIDArray) {
        _managePersonIDArray = [[NSMutableArray alloc]init];
    }
    [_managePersonIDArray removeAllObjects];
    for (HXSelectedManagePersonListModel *model in self.data) {//这里应该分开
        if ([model.isSelected isEqualToString:@"yes"]) {
            [_managePersonIDArray addObject:model.ID];
            [self.personNameArray addObject:model.PersonName];
        }
    }
    return _personIDArray;
}

@end
